package com.mock.action.organisation.department;

import java.util.ArrayList;
import java.util.List;

import com.mock.action.AbstractAction;
import com.mock.dao.BusinessTypeDao;
import com.mock.dao.CountryDao;
import com.mock.dao.DepartmentDao;
import com.mock.dao.impl.BusinessTypeDaoImpl;
import com.mock.dao.impl.DepartmentImpl;
import com.mock.model.BusinessType;
import com.mock.model.Country;
import com.mock.model.DataTable;
import com.mock.model.Department;
import com.opensymphony.xwork2.ActionSupport;

public class DepartmentAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<Department> listDepartment = new ArrayList<Department>();
	Long idDepartment;
	Long directorateId ;
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		DepartmentDao dao = new DepartmentImpl();
		
		
		DataTable dt = new DataTable();
		dt.sEcho = sEcho = getRequest().getParameter("sEcho");
		dt.iDisplayStart = iDisplayStart = Integer.parseInt(getRequest().getParameter("iDisplayStart"));
		dt.iDisplayLength = iDisplayLength = Integer.parseInt(getRequest().getParameter("iDisplayLength"));
		dt.sSearch = sSearch = getRequest().getParameter("sSearch");
		dt.sSearch_0 = sSearch_0 = getRequest().getParameter("sSearch_0");
		dt.sSearch_1 = sSearch_4 = getRequest().getParameter("sSearch_1");
		dt.sSearch_2 = sSearch_4 = getRequest().getParameter("sSearch_2");
		dt.sSearch_4 = sSearch_4 = getRequest().getParameter("sSearch_4");
		dt.iSortCol_0 = iSortCol_0 = Integer.parseInt(getRequest().getParameter("iSortCol_0"));
		dt.sSortDir_0 = sSortDir_0 = getRequest().getParameter("sSortDir_0");
		directorateId = Long.parseLong(getRequest().getParameter("directorateId"));
		dt.iTotalDisplayRecords = iTotalDisplayRecords = dao.getRowCount();
		dt.iTotalRecords = iTotalRecords = dao.getRowCount(Department.class);

		List<Department> list = dao.getDepartmentByDirectorateId(dt,directorateId);		
		for (Department bt : list) {
			ArrayList<String> newArray = new ArrayList<String>();
			newArray.add(bt.getDepartmentName());
			newArray.add(bt.getAddress1());
			newArray.add(bt.getPostcode());
			newArray.add(bt.getLeadContact().toString());
			if (bt.isActive()) newArray.add("Yes");
			else newArray.add("No");
			newArray.add(""+ bt.getId());
			
			aaData.add(newArray);
		}
		return SUCCESS;
		
	}
	
	
	
	public String changeActive() throws Exception {
		DepartmentDao dao = new DepartmentImpl();
		Department department ;
		try {
			department = dao.getById(Department.class, idDepartment);
			department.setActive(true);
			dao.update(department);
			directorateId = department.getDirectorate().getDirectorateId();
		} catch(Exception e){
			System.out.println("Exception : "+ e.getMessage());
		}
		
		return SUCCESS;
	}
	
	
	public String deActiveDepartment(){
		DepartmentDao dao = new DepartmentImpl();
		Department department ;
		try {
			department = dao.getById(Department.class, idDepartment);
			department.setActive(false);
			dao.update(department);
			directorateId = department.getDirectorate().getDirectorateId();
		} catch(Exception e){
			System.out.println("Exception : "+ e.getMessage());
		}
		
		return SUCCESS;
	}
	
	public String create(){
		return SUCCESS;
	}
	
	
	/**
	 * Getter+ setter
	 */
	public List<Department> getListDepartment() {
		return listDepartment;
	}
	public void setListDepartment(List<Department> listDepartment) {
		this.listDepartment = listDepartment;
	}

	public Long getIdDepartment() {
		return idDepartment;
	}

	public void setIdDepartment(Long idDepartment) {
		this.idDepartment = idDepartment;
	}

	public Long getDirectorateId() {
		return directorateId;
	}

	public void setDirectorateId(Long directorateId) {
		this.directorateId = directorateId;
	}
	

	private List<ArrayList<String>> aaData = new ArrayList<ArrayList<String>>();
	private String sEcho;
	private String sSearch;
	private int iDisplayLength;
	private int iDisplayStart = 0;
	private int iTotalRecords;
	private int iTotalDisplayRecords;
	private String sSearch_0;
	private int iSortCol_0;
	private String sSortDir_0;
	private String sSearch_4;


	public List<ArrayList<String>> getAaData() {
		return aaData;
	}
	public void setAaData(List<ArrayList<String>> aaData) {
		this.aaData = aaData;
	}
	public String getsEcho() {
		return sEcho;
	}
	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}
	public String getsSearch() {
		return sSearch;
	}
	public void setsSearch(String sSearch) {
		this.sSearch = sSearch;
	}
	public int getiDisplayLength() {
		return iDisplayLength;
	}
	public void setiDisplayLength(int iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
	}
	public int getiDisplayStart() {
		return iDisplayStart;
	}
	public void setiDisplayStart(int iDisplayStart) {
		this.iDisplayStart = iDisplayStart;
	}
	public int getiTotalRecords() {
		return iTotalRecords;
	}
	public void setiTotalRecords(int iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}
	public int getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}
	public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}
	public String getsSearch_0() {
		return sSearch_0;
	}
	public void setsSearch_0(String sSearch_0) {
		this.sSearch_0 = sSearch_0;
	}
	public int getiSortCol_0() {
		return iSortCol_0;
	}
	public void setiSortCol_0(int iSortCol_0) {
		this.iSortCol_0 = iSortCol_0;
	}
	public String getsSortDir_0() {
		return sSortDir_0;
	}
	public void setsSortDir_0(String sSortDir_0) {
		this.sSortDir_0 = sSortDir_0;
	}
	public String getsSearch_4() {
		return sSearch_4;
	}
	public void setsSearch_4(String sSearch_4) {
		this.sSearch_4 = sSearch_4;
	}

	

	
	
}
